package com.adong.tv.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.adong.tv.R;
import com.adong.tv.model.Movie;

import java.util.ArrayList;
import java.util.List;

/**
 * 剧集适配器
 */
public class EpisodeAdapter extends RecyclerView.Adapter<EpisodeAdapter.EpisodeViewHolder> {
    
    private List<Movie.Episode> episodes = new ArrayList<>();
    private final OnEpisodeClickListener listener;
    private int currentPlayingPosition = -1;
    
    public EpisodeAdapter(OnEpisodeClickListener listener) {
        this.listener = listener;
    }
    
    public void setEpisodes(List<Movie.Episode> episodes) {
        this.episodes = episodes;
        notifyDataSetChanged();
    }
    
    /**
     * 设置当前播放的集数
     * @param episode 当前播放集数
     */
    public void setCurrentPlayingEpisode(Movie.Episode episode) {
        int oldPosition = currentPlayingPosition;
        
        // 查找当前播放集数的位置
        for (int i = 0; i < episodes.size(); i++) {
            if (episodes.get(i).getUrl().equals(episode.getUrl())) {
                currentPlayingPosition = i;
                break;
            }
        }
        
        // 通知适配器更新
        if (oldPosition != -1) {
            notifyItemChanged(oldPosition);
        }
        if (currentPlayingPosition != -1) {
            notifyItemChanged(currentPlayingPosition);
        }
    }
    
    @NonNull
    @Override
    public EpisodeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_episode, parent, false);
        return new EpisodeViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull EpisodeViewHolder holder, int position) {
        Movie.Episode episode = episodes.get(position);
        holder.bind(episode, listener, position == currentPlayingPosition);
    }
    
    @Override
    public int getItemCount() {
        return episodes.size();
    }
    
    /**
     * 剧集视图持有者
     */
    static class EpisodeViewHolder extends RecyclerView.ViewHolder {
        private final TextView nameTextView;
        private final Context context;
        
        public EpisodeViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.tv_episode_name);
            context = itemView.getContext();
        }
        
        void bind(Movie.Episode episode, OnEpisodeClickListener listener, boolean isPlaying) {
            nameTextView.setText(episode.getName());
            
            // 设置背景颜色
            if (isPlaying) {
                nameTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.playing_episode));
                nameTextView.setTextColor(ContextCompat.getColor(context, R.color.black));
            } else {
                nameTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.card_background));
                nameTextView.setTextColor(ContextCompat.getColor(context, R.color.white));
            }
            
            // 设置焦点变化监听器
            itemView.setOnFocusChangeListener((v, hasFocus) -> {
                if (hasFocus) {
                    nameTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.card_selected));
                    nameTextView.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    if (isPlaying) {
                        nameTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.playing_episode));
                        nameTextView.setTextColor(ContextCompat.getColor(context, R.color.black));
                    } else {
                        nameTextView.setBackgroundColor(ContextCompat.getColor(context, R.color.card_background));
                        nameTextView.setTextColor(ContextCompat.getColor(context, R.color.white));
                    }
                }
            });
            
            itemView.setOnClickListener(v -> listener.onEpisodeClick(episode));
        }
    }
    
    /**
     * 剧集点击监听器
     */
    public interface OnEpisodeClickListener {
        void onEpisodeClick(Movie.Episode episode);
    }
} 